// pages/mch/car/list.js
const App = getApp()
import Kits from '../../../utils/kits'
import Util from '../../../utils/util'

Component({
  options: {
    addGlobalClass: true,
  },
  // 生命周期
  pageLifetimes:{
    show() {  // 对应页面的 onShow
      this.execLoad(true);  // 调用现有的数据加载方法
      console.log("shoWWW")
    },
   
  },
  properties: {
    cid: {
      type: String,
      default: ''
    },
    mid: {
      type: String,
      default: ''
    },
    state: {
      type: String,
      default: '0'
    }
  },
  data: {
    init: false,
    loading: false,
    triggered: true,
    CustomBar: App.Global.CustomBar,
    page: 1,
    size: 8,
    time: new Date().getTime(),
    pager: {
      now: 1,
      row: 0,
      all: 0,
      fms: 0,
    },
    items: []
  },

  lifetimes: {
    attached() {
      wx.sub('ReloadOrderList', this, () => {
        this.execLoad(false)
      })
      this.execLoad(false)
    },
    detached() {
      wx.cancel('ReloadOrderList', this)
    },
  },
  methods: {
    doPulldown() {
      this.data.page = 1
      this.execLoad(true)
    },
    doReachBottom() {
      if (this.data.page >= this.data.pager.all) {
        return
      }
      this.data.page = this.data.page + 1
      this.execLoad(true)
    },
    fmtMoney(coin) {
      let yuan = coin / 100
      if (parseInt(yuan) * 100 < coin) {
        let s = yuan.toFixed(2)
        if (yuan.toFixed(2).endsWith('0')) {
          return yuan.toFixed(1)
        }
        return s
      } else {
        return yuan.toFixed(0)
      }
    },
    toPage(e) {
      wx.navigateTo({
        url: e.currentTarget.dataset.uri
      })
    },
    execLoad(pulldown) {
      console.log(pulldown,"eqtewg")
      this.setData({
        loading: true
      })
      App.doPost({
        uri: '/app/merchant/order/list',
        body: {
          pager: {
            page: this.data.page,
            size: this.data.size,
            time: this.data.time
          },
          query: {
            state: parseInt(this.data.state),
            cid: this.data.cid,
            mid: this.data.mid
          }
        },
        fnSucc: resp => {
          if (resp.data.pager.now == 1) {
            this.data.time = resp.data.pager.fms
            this.triggerEvent('totalevent', {
              type: this.data.state == '0' ? 'all' : (this.data.state == '3' ? 'rund' : (this.data.state == '2' ? 'nos' : 'end')),
              value: resp.data.pager.row
            }, {})
          }
          resp.data.items.forEach(item => {
            item.hdays = Kits.getHourMemo(item.hours)
          })

          this.setData({
            init: true,
            loading: false,
            triggered: false,
            pager: resp.data.pager,
            items: resp.data.pager.now == 1 ? resp.data.items : this.data.items.concat(resp.data.items)
          })
        },
        fnComp: () => {
          if (pulldown) {
            this.triggerEvent('stopPullDown', {})
          }
        }
      })
    },

    doCancel(e) {
      let order = e.currentTarget.dataset.order
      wx.showModal({
        title: '取消提示',
        cancelText: '点错了',
        confirmText: '确认取消',
        confirmColor: '#e54d42',
        content: '是否取消订单？',
        success: (res) => {
          if (res.confirm) {
            App.doPost({
              uri: '/app/merchant/order/cancel',
              body: {
                id: order.id,
                memo: '门店取消订单'
              },
              fnSucc: () => {
                wx.pub("ReloadOrderList", {})

              }
            })
          }
        }
      })
    },
    doChange(e) {
      let order = e.currentTarget.dataset.order
      console.log(order)
      Util.Notify.warn(`暂不可调整租期`)

    }
  },
})